﻿Imports System.Windows.Forms
Imports System.Drawing
Imports Microsoft.VisualStudio.DebuggerVisualizers
Imports System.IO

<Assembly: System.Diagnostics.DebuggerVisualizer(
                GetType(Visualizers.FontVisualizer), GetType(Visualizers.FontVisualizerSource),
                Target:=GetType(System.Drawing.Font), Description:="Visualize as a TSQL Script"
    )> 
Namespace Visualizers
    Public Class FontVisualizer
        Inherits DialogDebuggerVisualizer

        Protected Overrides Sub Show(windowService As IDialogVisualizerService, objectProvider As IVisualizerObjectProvider)

            Dim Values() As String
            Using obj As New StreamReader(DirectCast(objectProvider.GetData, Stream))
                Values = obj.ReadToEnd().Split(",")
            End Using
            Using f As New FrmFontHost
                Dim fnt As New Font(Values(0).ToString, CType(Values(1), Decimal), CType(Values(2), FontStyle), GraphicsUnit.Point)
                f.Displayfont = fnt
                windowService.ShowDialog(f)
                fnt = f.Displayfont
                Dim ms As New MemoryStream
                Dim Sw As New StreamWriter(ms)
                Sw.Write(fnt.Name + ",")
                Sw.Write(fnt.SizeInPoints.ToString + ",")
                Sw.Write(fnt.Style)
                Sw.Flush()
                If MessageBox.Show("Are you sure you want to update the Unerlying Font?", "Font Visualizer", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1) = DialogResult.Yes Then
                    objectProvider.ReplaceData(ms)
                End If
            End Using
        End Sub

        Public Shared Sub TestFont(Dataobject As Object)
            Dim dv As New VisualizerDevelopmentHost(Dataobject, GetType(Visualizers.FontVisualizer), GetType(Visualizers.FontVisualizerSource))
            dv.ShowVisualizer()
        End Sub

    End Class

    Public Class FontVisualizerSource
        Inherits VisualizerObjectSource

        Public Overrides Sub GetData(target As Object, outgoingData As System.IO.Stream)
            Dim fnt As Font = DirectCast(target, Font)
            Dim Str As New StreamWriter(outgoingData)
            Str.Write(fnt.Name + ",")
            Str.Write(fnt.SizeInPoints.ToString + ",")
            Str.Write(fnt.Style)
            Str.Flush()
        End Sub

        Public Overrides Function CreateReplacementObject(target As Object, incomingData As System.IO.Stream) As Object
            Dim Values() As String
            Using obj As New StreamReader(incomingData)
                Values = obj.ReadToEnd().Split(",")
            End Using
            Return New Font(Values(0).ToString, CType(Values(1), Decimal), CType(Values(2), FontStyle), GraphicsUnit.Point)
        End Function

        Public Overrides Sub TransferData(target As Object, incomingData As System.IO.Stream, outgoingData As System.IO.Stream)
            MyBase.TransferData(target, incomingData, outgoingData)
        End Sub

    End Class

End Namespace

